import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_custom_dialog/flutter_custom_dialog.dart';
import 'package:oktoast/oktoast.dart';
import 'package:school_safe_teacher/constans/route_constants.dart';
import 'package:school_safe_teacher/resourse/app_colors.dart';
import 'package:school_safe_teacher/widget/my_widget.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    UserInfoState state, Dispatch dispatch, ViewService viewService) {
  return body(
      child: Container(
        color: AppColors.MainBackColor,
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.only(left: 15, right: 15),
              child:
                  noRightIconBar(context: viewService.context, title: "编辑资料"),
              color: Colors.white,
            ),
            Expanded(child: _body(state, dispatch, viewService)),
          ],
        ),
      ),
      context: viewService.context);
}

Widget _body(UserInfoState state, Dispatch dispatch, ViewService viewService) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.center,
    mainAxisSize: MainAxisSize.min,
    children: [
      GestureDetector(
        child: Column(
          children: [
            Container(
              child: CachedNetworkImage(
                width: 70,
                height: 70,
                imageUrl: state.userInfoEntity.headimg != null
                    ? state.userInfoEntity.headimg
                    : "",
                imageBuilder: (context, imageProvider) => Container(
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    image: DecorationImage(
                        image: imageProvider,
                        fit: BoxFit.cover,
                        colorFilter: ColorFilter.mode(
                            Colors.transparent, BlendMode.colorBurn)),
                  ),
                ),
                errorWidget: (context, url, error) =>
                    Image.asset("images/avator_default@2x.png"),
                fit: BoxFit.fitWidth,
              ),
              padding: EdgeInsets.only(
                top: 31,
              ),
            ),
            SizedBox(
              height: 15,
            ),
            Text(
              "点击修改头像",
              style: TextStyle(
                  fontSize: 13,
                  fontWeight: FontWeight.normal,
                  color: AppColors.middleTextColor),
            ),
            SizedBox(
              height: 31,
            )
          ],
        ),
        onTap: () {
          addPicDialog(state, dispatch, viewService);
        },
      ),
      Container(
        child: Column(
          children: [
            GestureDetector(
              child: Container(
                padding:
                    EdgeInsets.only(top: 10, bottom: 10, left: 25, right: 25),
                child: Row(
                  children: [
                    Text(
                      "昵称",
                      style: TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.normal,
                          color: AppColors.middleTextColor),
                    ),
                    Expanded(child: Text("")),
                    Text(
                      state.userInfoEntity.teacherName,
                      style: TextStyle(
                          fontWeight: FontWeight.normal,
                          fontSize: 15,
                          color: AppColors.deepTextColor),
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Image.asset(
                      "images/add_member_right@2x.png",
                      height: 10,
                      width: 5,
                    )
                  ],
                ),
              ),
              onTap: () {
                Navigator.of(viewService.context)
                    .pushNamed(RouteConstants.CHANGE_NICKNAME);
              },
            ),
            Divider(),
            GestureDetector(
              child: Container(
                padding:
                    EdgeInsets.only(top: 10, bottom: 10, left: 25, right: 25),
                child: Row(
                  children: [
                    Text(
                      "性别",
                      style: TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.normal,
                          color: AppColors.middleTextColor),
                    ),
                    Expanded(child: Text("")),
                    Text(
                      state.sex,
                      style: TextStyle(
                          fontWeight: FontWeight.normal,
                          fontSize: 15,
                          color: AppColors.deepTextColor),
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Image.asset(
                      "images/add_member_right@2x.png",
                      height: 10,
                      width: 5,
                    )
                  ],
                ),
              ),
              onTap: () {
                chooseSex(state, dispatch, viewService);
              },
            ),
            Divider(),
            GestureDetector(
              child: Container(
                padding:
                    EdgeInsets.only(top: 10, bottom: 10, left: 25, right: 25),
                child: Row(
                  children: [
                    Text(
                      "简介",
                      style: TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.normal,
                          color: AppColors.middleTextColor),
                    ),
                    Expanded(child: Text("")),
                    Text(
                      state.userInfoEntity.content,
                      style: TextStyle(
                          fontWeight: FontWeight.normal,
                          fontSize: 15,
                          color: AppColors.deepTextColor),
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Image.asset(
                      "images/add_member_right@2x.png",
                      height: 10,
                      width: 5,
                    )
                  ],
                ),
              ),
              onTap: () {
                Navigator.of(viewService.context)
                    .pushNamed(RouteConstants.CHANGE_USER_INFO);
              },
            ),
          ],
        ),
        color: Colors.white,
      )
    ],
  );
}

void chooseSex(
    UserInfoState state, Dispatch dispatch, ViewService viewService) {
  YYDialog yyDialog = YYDialog().build(viewService.context);
  yyDialog
    ..gravity = Gravity.bottom
    ..width = MediaQuery.of(viewService.context).size.width
    ..widget(Container(
      decoration: BoxDecoration(
          color: Colors.white,
          //设置四周圆角 角度
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(30), topRight: Radius.circular(30))),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            child: Container(
              child: Text("男"),
              margin: EdgeInsets.only(top: 15, bottom: 15),
            ),
            onTap: () {
              dispatch(UserInfoActionCreator.onChooseSex("1"));
              yyDialog.dismiss();
            },
          ),
          Divider(),
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            child: Container(
              child: Text("女"),
              margin: EdgeInsets.only(top: 15, bottom: 15),
            ),
            onTap: () {
              dispatch(UserInfoActionCreator.onChooseSex("2"));
              yyDialog.dismiss();
            },
          ),
          Platform.isIOS
              ? SizedBox(
                  height: MediaQuery.of(viewService.context).padding.bottom,
                )
              : Container()
        ],
      ),
    ))
    ..show();
}
YYDialog addPicDialog(
    UserInfoState state, Dispatch dispatch, ViewService viewService) {
  YYDialog yyDialog = YYDialog().build(viewService.context);
  return yyDialog.build(viewService.context)
    ..gravity = Gravity.bottom
    ..gravityAnimationEnable = true
    ..backgroundColor = Colors.white
    ..widget(GestureDetector(
      behavior: HitTestBehavior.opaque,
      child: Container(
        width: MediaQuery.of(viewService.context).size.width,
        padding: EdgeInsets.all(12),
        child: Center(
          child: Text("拍照"),
        ),
      ),
      onTap: () {
        dispatch(UserInfoActionCreator.onCameraPic());
        yyDialog.dismiss();
      },
    ))
    ..widget(GestureDetector(
      behavior: HitTestBehavior.opaque,
      child: Container(
        width: MediaQuery.of(viewService.context).size.width,
        padding: EdgeInsets.all(12),
        child: Center(
          child: Text("从相册中选取"),
        ),
      ),
      onTap: () {
        dispatch(UserInfoActionCreator.onChoosePic());
        yyDialog.dismiss();
      },
    ))
    ..widget(Platform.isIOS
        ? SizedBox(
      height: MediaQuery.of(viewService.context).padding.bottom,
    )
        : Container())
    ..show();
}
